package com.rlyy.http.servlet;

import lombok.SneakyThrows;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;

import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.Servlet;

/**
 * 添加ServletContext
 */
public class MyServletContext extends MockServletContext {

    private UrlMapper<ServletRegistration> servletUrlMapper = new UrlMapper<>(true);
    private UrlMapper<ServletFilterRegistration> filterUrlMapper = new UrlMapper<>(false);

    @SneakyThrows
    @Override
    public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
        MockServletConfig myServletConfig = new MockServletConfig();
        servlet.init(myServletConfig);
        ServletRegistration servletRegistration = new ServletRegistration(servletName,servlet,this,servletUrlMapper);
        return servletRegistration;
    }

    @Override
    public FilterRegistration.Dynamic addFilter(String filterName, Filter filter){
        ServletFilterRegistration registration = new ServletFilterRegistration(filterName,filter,this,filterUrlMapper);
        return registration;
    }
}
